/**
 * 
 */
package com.tju.worker;

/**
 * <p>
 * you should define your worker tasks by implementing <code>Runnable</code>.
 * this worker is intended to be used with a pool to control its life-cycle.
 * </p>
 * 
 */
public class Worker extends Thread {

	/**
	 * <p>
	 * master object for the worker that should be notified when job is done.
	 * </p>
	 */
	private Object master;

	private Runnable runnable;

	public Worker(Object master, Runnable runnable) {
		super("Worker-" + System.currentTimeMillis());
		this.master = master;
		this.runnable = runnable;
	}

	public void run() {
		synchronized (master) {
			runnable.run();
			sendSignal();
		}
	}

	public void sendSignal() {
		master.notifyAll();
	}
}
